<template>
  <el-card
    class="flex-grow-0 mr-1"
    shadow="always"
    :header="t('deng-guang-kong-zhi')"
    style="--el-card-padding: 0"
  >
    <div class="flex flex-col justify-between p-4 text-sm">
      <div class="grid gap-2 grid-cols-2 grid-rows-2 w-full">
        <template v-for="(item, index) in buttonList" :key="item.value">
          <el-button
            size="small"
            @click="() => onClickLight(item.value, index)"
            :style="changeBtnColor(index)"
          >
            {{ item.content }}
          </el-button>
        </template>
      </div>
      <div class="text-nowrap overflow-hidden text-sm">
        {{ t('zhuang-tai') }}：{{ lightStatus }}
      </div>
      <div class="flex items-center justify-between">
        <span>{{ t('ji-guang-zhi-shi') }}</span>
        <el-switch size="small" :model-value="disperseMode" @change="controlLaser"></el-switch>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { patrolingCruise } from '@/api/control'
import { useLightStore } from '@/business/light'
import { useBirdAway } from '@/composables/useBirdAway'
import { lightStatus } from '@/composables/useUpperControl'
import { useCarStore } from '@/stores/car'
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
const { disperseMode, controlLaser } = useBirdAway()

const { t } = useI18n()
const carStore = useCarStore()
const light = useLightStore()
// 按钮组合
const buttonList = [
  {
    value: 1,
    content: t('ju-guang-chang-liang')
  },
  {
    value: 2,
    content: t('ju-bao')
  },
  {
    value: 3,
    content: t('fan-chang')
  },
  {
    value: 4,
    content: t('fan-bao')
  },
  {
    value: 5,
    content: t('guan-deng')
  },
  {
    value: 8,
    content: t('quan-guan')
  }
]
const currentBtnIndex = ref<number | null>(null)
const changeBtnColor = (index: number) => {
  if (currentBtnIndex.value === index) {
    return {
      'background-color': '#ff931e',
      color: 'white'
    }
  }
}

async function onClickLight(value: number, index: number) {
  currentBtnIndex.value = index
  const data = {
    code: carStore.currentCar,
    param1: 7,
    param2: 1,
    param3: value,
    param4: 255
  }
  await patrolingCruise(data)
  light.onClickLightStatus()
}
</script>
<style scoped lang="scss">
:deep(.el-card__header) {
  padding: 0 0 0 10px;
  font-size: small;
  // border-bottom: none;
}
// :deep(.el-card__body) {
//   height: 100%;
//   padding: 20px;
// }
</style>
